package ink.xiaobaibai.serviceOxy.monthly.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import ink.xiaobaibai.bo.DeviceMqttBo;
import ink.xiaobaibai.entity.MonthlyOxygenDevice;
import ink.xiaobaibai.entity.MonthlyfeeDevice;
import ink.xiaobaibai.service.IMonthlyOxygenDeviceService;
import ink.xiaobaibai.service.IMonthlyUserDeviceService;
import ink.xiaobaibai.service.IMonthlyfeeDeviceService;
import ink.xiaobaibai.serviceOxy.IMqttService;
import ink.xiaobaibai.serviceOxy.monthly.IDelayMonControlService;
import ink.xiaobaibai.serviceOxy.monthly.IDeviceMonControlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description: 一把手控制层
 * @author: 小白白
 * @create: 2021-05-31
 **/

@Service
public class DeviceMonControlServiceImpl implements IDeviceMonControlService {

    @Autowired
    private IDelayMonControlService delayMonService;

    @Autowired
    private IMonthlyUserDeviceService userDeviceService;

    @Autowired
    private IMonthlyOxygenDeviceService oxygenDeviceService;

    @Autowired
    private IMonthlyfeeDeviceService deviceService;

    @Autowired
    private IMqttService mqttService;

    /**
     * 开启机器
     * 需要设置开启时长
     */
    @Override
    public void openDevice(Integer deviceId, Integer seconds) {
        MonthlyfeeDevice ozoneDevice = this.deviceService.getById(deviceId);
        MonthlyOxygenDevice oxygenDevice = this.oxygenDeviceService.getById(ozoneDevice.getOxygenId());
        //启动机器信号
        this.mqttService.openDevice(ozoneDevice.getDeviceCode(), new DeviceMqttBo(ozoneDevice.getDeviceCode(), true, ozoneDevice.getDelayedTime(), ozoneDevice.getFanTime(), ozoneDevice.getValveTime(), seconds));
        //氧气机工作时间=自己的工作时间
        this.mqttService.openDevice(oxygenDevice.getDeviceCode(), new DeviceMqttBo(oxygenDevice.getDeviceCode(), true, oxygenDevice.getDelayedTime(), oxygenDevice.getFanTime(), oxygenDevice.getValveTime(), seconds + ozoneDevice.getDelayedTime()));
    }

    /**
     *
     */
    @Override
    public void closeDeviceAtOnce(Integer deviceId) {
        QueryWrapper<MonthlyfeeDevice> q1 = new QueryWrapper<>();
        q1.select("device_code", "oxygen_id");
        q1.eq("id", deviceId);
        MonthlyfeeDevice monthlyfeeDevice = this.deviceService.getOne(q1);
        QueryWrapper<MonthlyOxygenDevice> q2 = new QueryWrapper<>();
        q2.select("device_code");
        q2.eq("id", monthlyfeeDevice.getOxygenId());
        MonthlyOxygenDevice oxygenDevice = this.oxygenDeviceService.getOne(q2);
        //关闭机器信号
        this.mqttService.closeDevice(monthlyfeeDevice.getDeviceCode());
        this.mqttService.closeDevice((oxygenDevice.getDeviceCode()));
    }

//    private void updateWorkStopTime(Integer deviceId, Date date) {
//        UpdateWrapper<MonthlyUserDevice> u1=new UpdateWrapper<>();
//        u1.set("work_stop_time",date);
//        u1.eq("device_id",deviceId);
//        userDeviceService.update(u1);
//    }

}
